﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Core.Validation;
using System.ComponentModel;
using Kingdee.BOS.App;
using Kingdee.BOS.Contracts;
using Kingdee.K3.BD.Contracts;
using Kingdee.BOS.App.Data;

namespace K.KDL
{
    [Description("其他入库单-保存操作")]
    public class CustomOtherInstockSaveOperationPlugin : AbstractOperationServicePlugIn
    {
        public override void OnPreparePropertys(PreparePropertysEventArgs e)
        {
            //e.FieldKeys.Add(""); 将需要应用的字段Key加入

            e.FieldKeys.Add("F_PATC_SCPH");  //明细：生产批号
            e.FieldKeys.Add("F_PATC_MJPH");  //明细：灭菌批号
            e.FieldKeys.Add("F_PATC_MJRQ"); //灭菌日期
            e.FieldKeys.Add("FLOT");  //FLOT

            e.FieldKeys.Add("F_PATC_SJSCRQ");  //实际生产日期
            e.FieldKeys.Add("F_PATC_SJSXQ");  //实际失效期
            e.FieldKeys.Add("F_PATC_FZSL");  //辅助数量

            e.FieldKeys.Add("F_BGP_APPROVALNO");  //注册证号
        }

        /// <summary>
        /// 添加校验器
        /// </summary>
        /// <param name="e"></param>
        public override void OnAddValidators(AddValidatorsEventArgs e)
        {
            var operValidator = new OperValidator();
            operValidator.AlwaysValidate = true;
            operValidator.EntityKey = "FBillHead";
            e.Validators.Add(operValidator);
        }

        /// <summary>
        /// 操作开始前功能处理
        /// </summary>
        /// <param name="e"></param>
        public override void BeginOperationTransaction(BeginOperationTransactionArgs e)
        {

        }

        /// <summary>
        /// 操作结束后功能处理
        /// </summary>
        /// <param name="e"></param>
        public override void EndOperationTransaction(EndOperationTransactionArgs e)
        {
            RefreshLotInfo(e);
        }

        void RefreshLotInfo(EndOperationTransactionArgs e)
        {
            var dbService = ServiceHelper.GetService<IDBService>();
            var cvtService = ServiceHelper.GetService<IUnitConvertService>();
            var entities = e.DataEntitys;
            foreach (var entity in entities)
            {
                var lines = entity["STK_MISCELLANEOUSENTRY"] as DynamicObjectCollection;
                if (lines == null || lines.Count() == 0)
                {
                    continue;
                }

                foreach (var line in lines)
                {
                    var id = Convert.ToInt32(line["Id"]);

                    var updateFlag = false;


                    var scph = line["F_PATC_SCPH"];
                    if (line["F_PATC_SCPH"] != null)
                    {
                        scph = Convert.ToString(line["F_PATC_SCPH"]);
                        updateFlag = true;
                    }
                    var mjph = line["F_PATC_MJPH"];
                    if (line["F_PATC_MJPH"] != null)
                    {
                        mjph = Convert.ToString(line["F_PATC_MJPH"]);
                        updateFlag = true;
                    }

                    var mjrq = line["F_PATC_MJRQ"];
                    if (line["F_PATC_MJRQ"] != null)
                    {
                        mjrq = Convert.ToString(line["F_PATC_MJRQ"]);
                        updateFlag = true;
                    }

                    var sjscrq = line["F_PATC_SJSCRQ"];
                    if (line["F_PATC_SJSCRQ"] != null)
                    {
                        sjscrq = Convert.ToString(line["F_PATC_SJSCRQ"]);
                        updateFlag = true;
                    }

                    var sjsxq = line["F_PATC_SJSXQ"];
                    if (line["F_PATC_SJSXQ"] != null)
                    {
                        sjsxq = Convert.ToString(line["F_PATC_SJSXQ"]);
                        updateFlag = true;
                    }

                    var fzsl = line["F_PATC_FZSL"];
                    if (line["F_PATC_FZSL"] != null)
                    {
                        fzsl = Convert.ToString(line["F_PATC_FZSL"]);
                        updateFlag = true;
                    }

                    var approvalNo = line["F_BGP_APPROVALNO"];
                    if (line["F_BGP_APPROVALNO"] != null)
                    {
                        approvalNo = Convert.ToString(line["F_BGP_APPROVALNO"]);
                        updateFlag = true;
                    }

                    if (line["Lot_Id"] == null || !updateFlag) continue;
                    var lotId = Convert.ToInt32(line["Lot_Id"]);
                    if (lotId <= 0) continue;

                    var preFlag = false;

                    var tempUpdate = "";
                    if (scph != null)
                    {
                        tempUpdate += " F_SHKD_MAJORLOT='" + scph + "' ";
                        preFlag = true;
                    }
                    if (mjph != null)
                    {
                        if (preFlag) tempUpdate += ",";
                        tempUpdate += " F_SHKD_MJLOT='" + mjph + "' ";
                        preFlag = true;
                    }
                    if (mjrq != null)
                    {
                        if (preFlag) tempUpdate += ",";
                        tempUpdate += " F_PATC_MJRQ='" + mjrq + "' ";
                        preFlag = true;
                    }

                    if (sjscrq != null)
                    {
                        if (preFlag) tempUpdate += ",";
                        tempUpdate += " F_PATC_SJSCRQ='" + sjscrq + "' ";
                        preFlag = true;
                    }
                    if (sjsxq != null)
                    {
                        if (preFlag) tempUpdate += ",";
                        tempUpdate += " F_PATC_SJSXQ='" + sjsxq + "' ";
                        preFlag = true;
                    }

                    if (fzsl != null)
                    {
                        if (preFlag) tempUpdate += ",";
                        tempUpdate += " F_PATC_FZSL=" + fzsl + " ";
                        preFlag = true;
                    }

                    if (approvalNo != null)
                    {
                        if (preFlag) tempUpdate += ",";
                        tempUpdate += " F_PATC_ZCZHDATE='" + approvalNo + "' ";
                        preFlag = true;
                    }

                    var sql = @"/*dialect*/ update T_BD_LOTMASTER
                                SET {0}
                                where FLOTID={1}";

                    DBUtils.ExecuteDynamicObject(Context, string.Format(sql, tempUpdate, lotId));
                }
            }
        }




        /// <summary>
        /// 当前操作的校验器
        /// </summary>
        private class OperValidator : AbstractValidator
        {
            public override void Validate(Kingdee.BOS.Core.ExtendedDataEntity[] dataEntities, ValidateContext validateContext, Kingdee.BOS.Context ctx)
            {
                //foreach (var dataEntity in dataEntities)
                //{
                //判断到数据有错误
                //    if()
                //    {
                //        ValidationErrorInfo ValidationErrorInfo = new ValidationErrorInfo(
                //            string.Empty,
                //            dataEntity["Id"].ToString(),
                //            dataEntity.DataEntityIndex,
                //            dataEntity.RowIndex,
                //            dataEntity["Id"].ToString(),
                //            "errMessage",
                //             string.Empty);
                //        validateContext.AddError(null, ValidationErrorInfo);
                //        continue;
                //    }

                //}
            }
        }

    }
}
